/**
 * useage
 * 
 * this.use("editor",{
 *      form:$("#formid")
 * });
 * 
 * //this.editor.bind("#formid");
 * 
 */

;(function(){

    var __editors = {}

    H5x.setEditor = function(name,editor){
        __editors[name] = editor;
    };

    H5x.getEditor = function(name){
        return __editors[name];
    }

	//var emitters_global = {};
	H5x.plugin("editor",function(component,opts){
		component.editor = {
            form:opts.form,
            init:init_form,
            setData:form_setdata,
            validate:validate_form,
            toJSON:form_tojson
        }
	});

    function init_form(data){
        if(data){
            form_setdata.apply(this,[data]);
        }
        //console.log(this.form);
        //console.log(this.form.find(".h5x-form-item"));
        this.form.find(".h5x-form-item").each(function(){
            var editor_name = $(this).attr("editor");
            var editor = H5x.getEditor(editor_name);
            if(editor){
                if(typeof editor.init == "function"){
                    editor.formitem = editor.init(this);
                }
            }else{
                console.log("can not found editor " + editor_name);
            }            
        });
    }

    function validate_form(){
        var errs = [];
        this.form.find(".h5x-form-item").each(function(){
            var editor_name = $(this).attr("editor");
            var editor = H5x.getEditor(editor_name);
            if(editor){
                if(typeof editor.validate == "function"){
                    var err = editor.validate(this);
                    if(err){
                        errs.push(err);
                    }                    
                }
            }else{
                console.log("can not found editor " + editor_name);
            }            
        });

        return errs;
    }

    function get_item_name(item){
        //debugger;
        if($(item).attr("name")){
            return $(item).attr("name");
        }
        return $(item).find("input,select,textarea").attr("name");
    }

    function form_tojson(){
        var data = {};
        this.form.find(".h5x-form-item").each(function(){
            var editor_name = $(this).attr("editor");
            var editor = H5x.getEditor(editor_name);
            if(editor){
                if(typeof editor.getter == "function"){
                    var val = editor.getter(this);
                    var item_name = get_item_name(this);
                    //console.log(item_name);
                    data[item_name] = val;             
                }
            }else{
                console.log("can not found editor " + editor_name);
            }            
        });
        return data;
    }

    function form_setdata(data){
        this.form.find(".h5x-form-item").each(function(){
            var editor_name = $(this).attr("editor");
            var editor = H5x.getEditor(editor_name);
            if(editor){
                if(typeof editor.setter == "function"){
                    var item_name = get_item_name(this);
                    editor.setter(this,data[item_name]);                  
                }
            }else{
                console.log("can not found editor " + editor_name);
            }            
        });
    }

    H5x.setEditor("input",{
        init:function(item){
            //return $(item).find("input");
        },
        setter:function(item,value){
            $(item).find("input,textarea").val(value);
        },
        getter:function(item){
            return $(item).find("input,textarea").val();
        },
        validate:function(item){
            /*
            if(!$(item).find("input").val()){
                return {
                    message:"${0}不能为空！"
                }
            }else{
                null;
            }*/
        }
    });

    H5x.setEditor("date-picker",{
        init:function(item){
            layui.use('laydate', function(){
               $(item).find("input").click(function(){
                    layui.laydate({
                        elem: this, 
                        istime: false,
                        format: 'YYYY-MM-DD'
                    })    
                    return false;
               });
                        
            });
        },
        setter:function(item,value){
            $(item).find("input").val(value);
        },
        getter:function(item){
            return $(item).find("input").val();
        },
        validate:function(item){
            /*
            if(!$(item).find("input").val()){
                return {
                    message:"${0}不能为空！"
                }
            }else{
                null;
            }*/
        }
    });

    H5x.setEditor("rich-text",{
        init:function(item){
            layui.use('layedit', function(){
                var editarea = $(item).find("textarea");
                editarea.attr("id","layedit-text");
                var index = layui.layedit.build("layedit-text");

                editarea.attr("editor-index",index);

            });
        },
        setter:function(item,value){
            $(item).find("textarea").val(value);
        },
        getter:function(item){
            var index = $(item).find("textarea").attr("editor-index");
            return layui.layedit.getContent(index);                
        },
        validate:function(item){
            /*
            if(!$(item).find("input").val()){
                return {
                    message:"${0}不能为空！"
                }
            }else{
                null;
            }*/
        }
    });

    H5x.setEditor("image-upload",{
        init:function(item){    
            $(item).find("button").click(function(){
                alert("upppp");
            });
        },
        setter:function(item,value){
            $(item).find("input").val(value);
        },
        getter:function(item){         
            return $(item).find("input").val();
        },
        validate:function(item){
            /*
            if(!$(item).find("input").val()){
                return {
                    message:"${0}不能为空！"
                }
            }else{
                null;
            }*/
        }
    });


})();
